package com.frank.jpaSpecification.service;

import com.frank.jpaSpecification.entity.Mobile;
import com.frank.jpaSpecification.entity.PageParamRequest;
import com.frank.jpaSpecification.repository.MobileRepository;
import com.frank.jpaSpecification.specification.MobileSpecification;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小石潭记
 * @date 2020/12/12 14:22
 * @Description: ${todo}
 */
@Service
public class MobileService {

    @Autowired
    private MobileRepository mobileRepository;

    @Autowired
    private MobileSpecification mobileSpecification;

    public Page<Mobile> getAllMobile(PageParamRequest pageParamRequest) {
        // 根据价格降序
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = PageRequest.of(pageParamRequest.getPageNumber(), pageParamRequest.getPageSize(), sort);
        return mobileRepository.findAll(mobileSpecification.getMobileSpecification(pageParamRequest), pageRequest);
    }

}
